<?php
class Advertises_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
		$this->ciObject->load->model('outgoingincoming_model');
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_advertises_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_outgoing_incoming._currencies_rid as _currencies_rid,
							_advertises_rows._cities_rid as _cities_rid,
							_advertises_rows._advertisessources_rid as _advertisessources_rid,
							_advertises_rows._advertisescompanies_rid as _advertisescompanies_rid,
							_outgoing_incoming._filials_rid as _filials_rid,
							DATE_FORMAT(_advertises_rows.bdate, \'%d.%m.%Y\') as bdate,
							DATE_FORMAT(_advertises_rows.edate, \'%d.%m.%Y\') as edate,
							_outgoing_incoming.summ_value as summ_value,
							DATE_FORMAT(_outgoing_incoming.oper_date, \'%d.%m.%Y\') as oper_date,
							_currencies.code as currency_code,
							_cities.city_name as city_name,
							_filials.name as filial_name,
							_advertisescompanies.company_name as company_name,
							_advertisessources.source_name as source_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_outgoing_incoming', '_documents.rid = _outgoing_incoming._documents_rid');
		$this->db->join('_advertises_headers', '_advertises_headers._documents_rid = _documents.rid');
		$this->db->join('_advertises_rows', '_advertises_rows._advertises_headers_rid = _advertises_headers.rid');
		$this->db->join('_currencies', '_outgoing_incoming._currencies_rid = _currencies.rid');
		$this->db->join('_cities', '_advertises_rows._cities_rid = _cities.rid');
		$this->db->join('_filials', '_outgoing_incoming._filials_rid = _filials.rid', 'LEFT');
		$this->db->join('_advertisessources', '_advertises_rows._advertisessources_rid = _advertisessources.rid');
		$this->db->join('_advertisescompanies', '_advertises_rows._advertisescompanies_rid = _advertisescompanies.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$whereArr = array();
			if(isset($searchRule['_advertises_headers.date_doc>='])) { $whereArr['_officialcources_headers.date_doc>='] = $this->toMySQLDate($searchRule['_officialcources_headers.date_doc>=']); unset($searchRule['_advertises_headers.date_doc>=']); } 
			if(isset($searchRule['_advertises_headers.date_doc<='])) { $whereArr['_officialcources_headers.date_doc<='] = $this->toMySQLDate($searchRule['_officialcources_headers.date_doc<=']); unset($searchRule['_advertises_headers.date_doc<=']); } 
			//var_dump($searchRule);
			if($whereArr)  $this->db->where($whereArr);
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_advertises_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_outgoing_incoming._currencies_rid as _currencies_rid,
							_outgoing_incoming.oper_type as oper_type,
							_advertises_rows._cities_rid as _cities_rid,
							_advertises_rows.advertises_name as advertises_name,
							_advertises_rows._advertisessources_rid as _advertisessources_rid,
							_advertises_rows._advertisescompanies_rid as _advertisescompanies_rid,
							_outgoing_incoming._filials_rid as _filials_rid,
							DATE_FORMAT(_advertises_rows.bdate, \'%d.%m.%Y\') as bdate,
							DATE_FORMAT(_advertises_rows.edate, \'%d.%m.%Y\') as edate,
							_outgoing_incoming.summ_value as summ_value,
							DATE_FORMAT(_outgoing_incoming.oper_date, \'%d.%m.%Y\') as oper_date,
							_currencies.code as currency_code,
							_cities.city_name as city_name,
							_filials.name as filial_name,
							_advertisescompanies.company_name as company_name,
							_advertisessources.source_name as source_name,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_outgoing_incoming', '_documents.rid = _outgoing_incoming._documents_rid');
		$this->db->join('_advertises_headers', '_advertises_headers._documents_rid = _documents.rid');
		$this->db->join('_advertises_rows', '_advertises_rows._advertises_headers_rid = _advertises_headers.rid');
		$this->db->join('_currencies', '_outgoing_incoming._currencies_rid = _currencies.rid');
		$this->db->join('_cities', '_advertises_rows._cities_rid = _cities.rid');
		$this->db->join('_filials', '_outgoing_incoming._filials_rid = _filials.rid', 'LEFT');
		$this->db->join('_advertisessources', '_advertises_rows._advertisessources_rid = _advertisessources.rid');
		$this->db->join('_advertisescompanies', '_advertises_rows._advertisescompanies_rid = _advertisescompanies.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
							'_documents_rid'=>$docRid,
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_advertises_headers', $insHeaderArray);
		$insRowsArray = array('_advertises_headers_rid'=>$this->db->insert_id(),
							'_cities_rid'=>$_POST['_cities_rid'],
							'advertises_name'=>$_POST['advertises_name'],
							'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
							'_advertisescompanies_rid'=>$_POST['_advertisescompanies_rid'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'edate'=>$this->toMySQLDate($_POST['edate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_advertises_rows', $insRowsArray);
		$this->ciObject->outgoingincoming_model->AddFinOper($docRid, $this->toMySQLDate($_POST['oper_date']), '00002', $_POST['summ_value'], $_POST['_currencies_rid'], $_POST['_filials_rid'], isset($_POST['oper_type'])?$_POST['oper_type']:'P');
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_advertises_headers', $updateHeaderArray, array('_advertises_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_advertises_headers', array('_advertises_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_cities_rid'=>$_POST['_cities_rid'],
							'advertises_name'=>$_POST['advertises_name'],
							'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
							'_advertisescompanies_rid'=>$_POST['_advertisescompanies_rid'],
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'edate'=>$this->toMySQLDate($_POST['edate']),
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_advertises_rows', $updateRowsArray, array('_advertises_rows._advertises_headers_rid'=>$row->rid));
		$this->ciObject->outgoingincoming_model->DelFinOper($this->ciObject->GetSessionData('activerecord'));
		$this->ciObject->outgoingincoming_model->AddFinOper($this->ciObject->GetSessionData('activerecord'), $this->toMySQLDate($_POST['oper_date']), '00002', $_POST['summ_value'], $_POST['_currencies_rid'], $_POST['_filials_rid'], isset($_POST['oper_type'])?$_POST['oper_type']:'P');
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetPair($p_value = 'rid', $p_name='company_name'){
		$this->db->select("_advertisescompanies.{$p_value}, _advertisescompanies.{$p_name}");
		$this->db->from('_advertisescompanies');
		$this->db->orderby("_advertisescompanies.{$p_name}");
		$query = $this->db->get();
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetPairArr($p_value = 'rid', $p_name='company_name'){
		$this->db->select("_advertisescompanies.{$p_value}, _advertisescompanies.{$p_name}");
		$this->db->from('_advertisescompanies');
		$this->db->orderby("_advertisescompanies.{$p_name}");
		$query = $this->db->get();
		if($query->num_rows()>0) {
			$result = array(' '=>'');
			foreach($query->result() as $item) $result[$item->$p_value]=$item->$p_name;
			return $result;
		}
		return False;
	}
	
}
?>